﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PowerArrow.nasa.Model;
using SilverlightCairngorm.Control;
using PowerArrow.nasa.Control;

namespace PowerArrow.nasa.View.Compont
{
    public partial class BackGround : UserControl
    {

        private Storyboard sb0 = new Storyboard();
        private Storyboard sb1 = new Storyboard();
        private Storyboard sb2 = new Storyboard();
        private DoubleAnimation wa0 = new DoubleAnimation();
        private DoubleAnimation wa1 = new DoubleAnimation();
        private DoubleAnimation wa2 = new DoubleAnimation();

        public BackGround()
        {
            // Required to initialize variables
            InitializeComponent();

            InitStoryBoard();
        }

        private void InitStoryBoard()
        {
            sb0.RepeatBehavior = RepeatBehavior.Forever;
            sb1.RepeatBehavior = RepeatBehavior.Forever;
            sb2.RepeatBehavior = new RepeatBehavior(1);

            wa0.Duration = new Duration(TimeSpan.FromSeconds(14));
            //wa0.From = 0;
            wa0.To = -800;
            Storyboard.SetTarget(wa0, bg0);
            Storyboard.SetTargetProperty(wa0, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));
            sb0.Children.Add(wa0);

            wa1.Duration = new Duration(TimeSpan.FromSeconds(10));
            //wa1.From = 0;
            wa1.To = -800;
            Storyboard.SetTarget(wa1, bg1);
            Storyboard.SetTargetProperty(wa1, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));
            sb1.Children.Add(wa1);


            wa2.Duration = new Duration(TimeSpan.FromSeconds(10));
            //wa2.From = 0;
            wa2.To = -1000;
            Storyboard.SetTarget(wa2, xTranslateTransform);
            Storyboard.SetTargetProperty(wa2, new PropertyPath("TranslateTransform.X"));
            sb2.Children.Add(wa2);


            this.Resources.Add("sb0", sb0);
            this.Resources.Add("sb1", sb1);
            this.Resources.Add("sb2", sb2);


            sb2.Completed += new EventHandler(sb2_Completed);
        }

        void sb2_Completed(object sender, EventArgs e)
        {
            this.Status = 1;
        }

        #region StatusProperty
        public int Status
        {
            get { return (int)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(int), typeof(BackGround), new PropertyMetadata(2, OnStatusPropertyChanged));

        private static void OnStatusPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BackGround backGround = d as BackGround;
            backGround.StatusChanged((int)e.NewValue);
        }

        private void StatusChanged(int s)
        {
            if (s == 0)
            {
                Run();
            }
            else if (s == 1)
            {
                Pause();
            }
            else if (s == 2)
            {
                Stop();
            }
            else if (s == 3)
            {
                Resume();
            }
        }
        #endregion

        #region ChkResultProperty
        public bool ChkResult
        {
            get { return (bool)GetValue(ChkResultProperty); }
            set { SetValue(ChkResultProperty, value); }
        }
        public static readonly DependencyProperty ChkResultProperty =
            DependencyProperty.Register("ChkResult", typeof(bool), typeof(BackGround), new PropertyMetadata(false, OnChkResultPropertyChanged));

        private static void OnChkResultPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BackGround backGround = d as BackGround;
            backGround.ChkResultChanged((bool)e.NewValue);
        }

        private void ChkResultChanged(bool b)
        {
            PowerArrowModel model = PowerArrowModel.Instance;
            if (b)
            {
                model.CurrResult = DoChkResult();
                model.ChkResult = false;
                if (model.CurrResult != 0)
                {
                }
                else
                {
                    model.ShowArrow = false;
                }
                model.BackgroundStatus = 1;
                model.ArrStatus = ArrowViewStatus.General.ToString();

                if (model.CurrS == 1)
                {
                    model.S1 = model.CurrResult;
                }
                else if (model.CurrS == 2)
                {
                    model.S2 = model.CurrResult;
                }
                else if (model.CurrS == 3)
                {
                    model.S3 = model.CurrResult;
                }
                else if (model.CurrS == 4)
                {
                    model.S4 = model.CurrResult;
                }
                else if (model.CurrS == 5)
                {
                    model.S5 = model.CurrResult;
                }
                else if (model.CurrS == 6)
                {
                    model.S6 = model.CurrResult;
                }

                model.TotalScore += model.CurrResult;
                model.CurrS++;
                model.IsFlying = false;

                if (model.CurrS > 6) {
                    if (model.TotalScore >= model.Mission)
                    {
                        PopPanel pp = new PopPanel();
                        pp.Txt = "恭喜过关!";
                        pp.Show(PopPanelStyle.ModalDimmed);

                        CairngormEvent onReadyEvt = new CairngormEvent(PowerArrowController.ARR_EVENT_OFFFIRE);
                        onReadyEvt.dispatch();
                    }
                    else
                    {
                        PopPanel pp = new PopPanel();
                        pp.Txt = "失败!   重新挑战！";
                        pp.Show(PopPanelStyle.ModalDimmed);

                        CairngormEvent onReadyEvt = new CairngormEvent(PowerArrowController.ARR_EVENT_OFFFIRE);
                        onReadyEvt.dispatch();

                        model.Level = model.Level - 1;
                    }
                }
            }
        }

        public int DoChkResult()
        {
            int res = 0;
            double x = 640 + xTranslateTransform.X + 55;
            //res = Convert.ToInt32(x);
            //10    60  70
            //9     55  75
            //8     50  80
            //7     45  85
            //6     40  90
            //5     35  95
            //4     30  100
            //3     25  105
            //2     20  110
            //1     15  115
            if (x >= 60 && x <= 70) { res = 10; }
            if ((x < 60 && x >= 55) || (x > 70 && x <= 75)) { res = 9; }
            if ((x < 55 && x >= 50) || (x > 75 && x <= 80)) { res = 8; }
            if ((x < 50 && x >= 45) || (x > 80 && x <= 85)) { res = 7; }
            if ((x < 45 && x >= 40) || (x > 85 && x <= 90)) { res = 6; }
            if ((x < 40 && x >= 35) || (x > 90 && x <= 95)) { res = 5; }
            if ((x < 35 && x >= 30) || (x > 95 && x <= 100)) { res = 4; }
            if ((x < 30 && x >= 25) || (x > 100 && x <= 105)) { res = 3; }
            if ((x < 25 && x >= 20) || (x > 105 && x <= 110)) { res = 2; }
            if ((x < 20 && x >= 15) || (x > 110 && x <= 115)) { res = 1; }


            return res;
        }
        #endregion

        #region LevelProperty
        public int Level
        {
            get { return (int)GetValue(LevelProperty); }
            set { SetValue(LevelProperty, value); }
        }
        public static readonly DependencyProperty LevelProperty =
            DependencyProperty.Register("Level", typeof(int), typeof(BackGround), new PropertyMetadata(1, OnLevelPropertyChanged));

        private static void OnLevelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BackGround backGround = d as BackGround;
            backGround.LevelChanged((int)e.NewValue);
        }

        private void LevelChanged(int l)
        {
            wa0.Duration = new Duration(TimeSpan.FromSeconds((14 - l * 0.5)));
            wa1.Duration = new Duration(TimeSpan.FromSeconds((10 - l * 0.5)));
            wa2.Duration = new Duration(TimeSpan.FromSeconds((10 - l * 0.5)));
            PowerArrowModel model = PowerArrowModel.Instance;
            model.Mission = l * 7;
        }
        #endregion

        #region bgMethod
        private void Run()
        {
            PowerArrowModel model = PowerArrowModel.Instance;
            if (model.CurrS > 6)
            {
                model.CurrS = 1;
                model.S1 = 11;
                model.S2 = 11;
                model.S3 = 11;
                model.S4 = 11;
                model.S5 = 11;
                model.S6 = 11;
                model.TotalScore = 0;
                model.Level++;
            }
            if (model.Level > 8) {
                PopPanel pp = new PopPanel();
                pp.Txt = "好厉害  !  佩服你  ：）";
                pp.IsShowBtn = false;
                pp.Show(PopPanelStyle.ModalDimmed);
            }

            sb0.Begin();
            sb1.Begin();
            sb2.Begin();
        }

        private void Pause()
        {
            sb0.Pause();
            sb1.Pause();
            sb2.Pause();
        }

        private void Stop()
        {
            sb0.Stop();
            sb1.Stop();
            sb2.Stop();
        }

        private void Resume()
        {
            sb0.Resume();
            sb1.Resume();
            sb2.Resume();
        }
        #endregion
    }
}